package com.customers.dao.Impl;

import com.customers.dao.CustomerDao;
import com.customers.entity.Customer;
import com.customers.entity.MainForm;
import com.customers.util.ConnectJDBC;
import com.customers.util.EncapsulationUtil;
import org.dom4j.Element;

import java.lang.reflect.InvocationTargetException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CustomerDaoImpl implements CustomerDao {
    private Map<String, String> sqlMap;

    public Map<String, String> getSqlMap() {
        return sqlMap;
    }

    @Override
    public String toString() {
        return "CustomerDaoImpl{" +
                "sqlMap=" + sqlMap +
                '}';
    }

    public void setSqlMap(Map<String, String> sqlMap) {
        this.sqlMap = sqlMap;
    }

    @Override
    public void editFormSave(MainForm form, String sql) throws SQLException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        //        String sql = "update tb_master set name=? ,unit=?,classify=?,source=?,cus_address=?,serve=?,servetype=?,address=?,level=?," +
//                "division_code=?,zip_code=?,short=?,j_person=?,organization=?,web=?,status=?,phone=? where id = ?";
        PreparedStatement ps = null;
        sql = sqlMap.get(sql);
        try {
//            ps = ConnectJDBC.connectDB(sql);

            ps = EncapsulationUtil.objectIntoPs(form, sql, ps);
//            ps.setString(1,form.getName());
//            ps.setString(2,form.getUnit());
//            ps.setString(3,form.getClassify());
//            ps.setString(4,form.getSource());
//            ps.setString(5,form.getCus_address());
//            ps.setString(6,form.getServe());
//            ps.setString(7,form.getServetype());
//            ps.setString(8,form.getAddress());
//            ps.setString(9,form.getLevel());
//            ps.setString(10,form.getDivision_code());
//            ps.setString(11,form.getZip_code());
//            ps.setString(12,form.getShortname());
//            ps.setString(13,form.getJ_person());
//            ps.setString(14,form.getOrganization());
//            ps.setString(15,form.getWeb());
//            ps.setString(16,form.getStatus());
//            ps.setString(17,form.getPhone());
//            ps.setInt(18,form.getId());

            ps.executeUpdate();
        } catch (Exception e) {
            throw e;
        } finally {
            if (ps != null) {
                try {
                    ps.close();
                } catch (Exception e) {
                    throw e;
                }
            }
        }
    }

    @Override
    public void deleteCustomer(MainForm form, String sql) throws SQLException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
//        String sql = "delete from tb_master where id = ?";
        PreparedStatement ps = null;
        sql = sqlMap.get(sql);
        try {
            ps = EncapsulationUtil.objectIntoPs(form, sql, ps);
//            ps = ConnectJDBC.connectDB(sql);
//            ps.setInt(1, form.getId());
            ps.executeUpdate();
        } catch (Exception e) {
            throw e;
        } finally {
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    throw e;
                }
            }
        }
    }

    @Override
    public void formOperate(MainForm form, String customerSql) throws SQLException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
//        String sql = "insert into tb_master (id,name,unit,classify,source,cus_address,serve,servetype,address,level,division_code," +
//                "zip_code,short,j_person,organization,web,status,phone) values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        PreparedStatement ps = null;
        String sql = sqlMap.get(customerSql);
        try {
//            ps = ConnectJDBC.connectDB(sql);

            ps = EncapsulationUtil.objectIntoPs(form, sql, ps);
//            ps.setString(1,form.getName());
//            ps.setString(2,form.getUnit());
//            ps.setString(3,form.getClassify());
//            ps.setString(4,form.getSource());
//            ps.setString(5,form.getCus_address());
//            ps.setString(6,form.getServe());
//            ps.setString(7,form.getServetype());
//            ps.setString(8,form.getAddress());
//            ps.setString(9,form.getLevel());
//            ps.setString(10,form.getDivision_code());
//            ps.setString(11,form.getZip_code());
//            ps.setString(12,form.getShortname());
//            ps.setString(13,form.getJ_person());
//            ps.setString(14,form.getOrganization());
//            ps.setString(15,form.getWeb());
//            ps.setString(16,form.getStatus());
//            ps.setString(17,form.getPhone());

            ps.executeUpdate();
        } catch (Exception e) {
            throw e;
        } finally {
            if (ps != null) {
                try {
                    ps.close();
                } catch (Exception e) {
                    throw e;
                }
            }
        }
    }

    @Override
    public List<MainForm> searchAllCustomer(String sql) throws SQLException {
        //        String sql = "select * from tb_master";
        PreparedStatement ps = null;
        List<MainForm> forms = new ArrayList<>();
        ResultSet rs = null;
        sql = sqlMap.get(sql);
        try {
            ps = ConnectJDBC.connectDB(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                MainForm form = new MainForm();

                form.setId(Integer.parseInt(rs.getString(1)));
                form.setName(rs.getString(2));
                form.setUnit(rs.getString(3));
                form.setClassify(rs.getString(4));
                form.setSource(rs.getString(5));
                form.setCus_address(rs.getString(6));
                form.setServe(rs.getString(7));
                form.setServetype(rs.getString(8));
                form.setAddress(rs.getString(9));
                form.setLevel(rs.getString(10));
                form.setDivision_code(rs.getString(11));
                form.setZip_code(rs.getString(12));
                form.setShortname(rs.getString(13));
                form.setJ_person(rs.getString(14));
                form.setOrganization(rs.getString(15));
                form.setWeb(rs.getString(16));
                form.setStatus(rs.getString(17));
                form.setPhone(rs.getString(18));

                forms.add(form);
            }

        } catch (SQLException e) {
            throw e;
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    throw e;
                }
            }
            if (ps != null) {
                try {

                    ps.close();
                } catch (SQLException e) {
                    throw e;
                }
            }

        }
        return forms;
    }

    @Override
    public List<MainForm> searchByName(String name, String sql) throws SQLException {
        //        String sql = "select * from tb_master where name like ?";
        PreparedStatement ps = null;
        List<MainForm> forms = new ArrayList<>();
        sql = sqlMap.get(sql);
        ResultSet rs = null;
        try {
            ps = ConnectJDBC.connectDB(sql);
            ps.setString(1, "%" + name + "%");

            rs = ps.executeQuery();
            while (rs.next()) {
                MainForm form = new MainForm();
                System.out.println("id = " + rs.getString(1));
                form.setId(Integer.parseInt(rs.getString(1)));
                form.setName(rs.getString(2));
                form.setUnit(rs.getString(3));
                form.setClassify(rs.getString(4));
                form.setSource(rs.getString(5));
                form.setCus_address(rs.getString(6));
                form.setServe(rs.getString(7));
                form.setServetype(rs.getString(8));
                form.setAddress(rs.getString(9));
                form.setLevel(rs.getString(10));
                form.setDivision_code(rs.getString(11));
                form.setZip_code(rs.getString(12));
                form.setShortname(rs.getString(13));
                form.setJ_person(rs.getString(14));
                form.setOrganization(rs.getString(15));
                form.setWeb(rs.getString(16));
                form.setStatus(rs.getString(17));
                form.setPhone(rs.getString(18));

                forms.add(form);
            }

        } catch (SQLException e) {
            throw e;
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    throw e;
                }
            }
            if (ps != null) {
                try {

                    ps.close();
                } catch (SQLException e) {
                    throw e;
                }
            }

        }
        return forms;
    }

    @Override
    public Integer searchIdByName(MainForm form, String getIdSql) throws SQLException {
        Integer id = null;
        ResultSet rs = null;
//        String sql = "select id from tb_master where name = ? ";
        PreparedStatement ps = null;
        String sql = sqlMap.get(getIdSql);
        try {
            String name = form.getName();
            ps = ConnectJDBC.connectDB(sql);
            ps.setString(1, name);
            rs = ps.executeQuery();
            if (rs.next()) {
                id = Integer.parseInt(rs.getString(1));
            }
        } catch (Exception e) {
            throw e;
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    throw e;
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    throw e;
                }
            }
        }
        return id;
    }

    @Override
    public void addOperate(List<Customer> customers, Integer id, String contactSql) throws SQLException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
//        String sql = "INSERT INTO tb_contact (id,name,department,posts,phone_number,personnel,recent_time,wechat,qq,dept_id) values(null,?,?,?,?,?,?,?,?,?)";
        String sql = sqlMap.get(contactSql);
        PreparedStatement ps = null;
        try {


            ps = ConnectJDBC.connectDB(sql);
            for (int i = 0; i < customers.size(); i++) {
                ps = EncapsulationUtil.objectIntoPs(customers.get(i), sql, ps);
//                ps.setString(1, customers.get(i).getName());
//                ps.setString(2, customers.get(i).getDepartment());
//                ps.setString(3, customers.get(i).getPosts());
//                ps.setString(4, customers.get(i).getPhone());
//                ps.setString(5, customers.get(i).getPersonnel());
//                ps.setString(6, customers.get(i).getRecenttime());
//                ps.setString(7, customers.get(i).getWechat());
//                ps.setString(8, customers.get(i).getQq());
//                ps.setInt(9, id);
                ps.executeUpdate();
            }

        } catch (Exception e) {
            throw e;
        } finally {
            if (ps != null) {
                try {
                    ps.close();
                } catch (Exception ex) {
                    throw ex;
                }
            }
        }
    }

    @Override
    public void editContactSave(Customer form, String sql) throws SQLException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
//        String sql = "update tb_contact set name=? ,department=?,posts=?,phone_number=?,personnel=?,recent_time=? ,wechat=?, qq=? where id = ?";
        PreparedStatement ps = null;
        sql = sqlMap.get(sql);
        try {

            ps = EncapsulationUtil.objectIntoPs(form, sql, ps);
//            ps = ConnectJDBC.connectDB(sql);
//
//            ps.setString(1,form.getName());
//            ps.setString(2,form.getDepartment());
//            ps.setString(3,form.getPosts());
//            ps.setString(4,form.getPhone());
//            ps.setString(5,form.getPersonnel());
//            ps.setString(6,form.getRecenttime());
//            ps.setString(7,form.getWechat());
//            ps.setString(8,form.getQq());
//            ps.setInt(9,form.getId());

            ps.executeUpdate();

        } catch (Exception e) {
            throw e;
        } finally {
            if (ps != null) {
                try {
                    ps.close();
                } catch (Exception e) {
                    throw e;
                }
            }
        }
    }

    @Override
    public List<Customer> searchById(Integer id, String sql) throws SQLException {
        //        String sql = "select * from tb_contact where dept_id = ?";
        PreparedStatement ps = null;
        List<Customer> forms = new ArrayList<>();
        ResultSet rs = null;
        sql = sqlMap.get(sql);
        try {
            ps = ConnectJDBC.connectDB(sql);
            ps.setInt(1, id);

            rs = ps.executeQuery();
            while (rs.next()) {
                Customer form = new Customer();
                form.setId(Integer.parseInt(rs.getString(1)));
                form.setName(rs.getString(2));
                form.setDepartment(rs.getString(3));
                form.setPosts(rs.getString(4));
                form.setPhone(rs.getString(5));
                form.setPersonnel(rs.getString(6));
                form.setRecenttime(rs.getString(7));
                form.setWechat(rs.getString(8));
                form.setQq(rs.getString(9));
                form.setDept_id(rs.getInt(10));

                forms.add(form);
            }
        } catch (SQLException e) {
            throw e;
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    throw e;
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    throw e;
                }
            }

        }
        return forms;
    }

    @Override
    public int searchIdByName(String name) {
        int id = 0;
        ResultSet rs = null;
//        String sql = "select id from tb_contact where name = ?";
        PreparedStatement ps = null;
        String sql = null;
        List<Element> elements = ConnectJDBC.getsqlElement().elements();
        for (Element element : elements) {
            if (element.elementText("contactByName") != null) {
                sql = element.elementText("contactByName").trim();
            }
        }
        try {
            ps = ConnectJDBC.connectDB(sql);
            ps.setString(1, name);
            rs = ps.executeQuery();
            if (rs.next()) {
                id = Integer.parseInt(rs.getString(1));
            }
        } catch (Exception e) {
            throw new RuntimeException();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return id;
    }

    @Override
    public void deleteContact(Customer customer, String sql) throws SQLException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
//        String sql = "delete from tb_contact where id = ?";
        PreparedStatement ps = null;
        sql = sqlMap.get(sql);
        try {
            ps = EncapsulationUtil.objectIntoPs(customer,sql,ps);
//            ps = ConnectJDBC.connectDB(sql);
//            ps.setInt(1, customer.getId());
            ps.executeUpdate();
        } catch (Exception e) {
            ConnectJDBC.getConnection().rollback();
            throw e;
        } finally {
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    throw e;
                }
            }
        }
    }
}
